import { observable, action } from 'mobx';
import { ListAdvanced, ListViewPage, SearchAdvanced, RouteLayout } from 'weaver-mobile-page';
import { Tools, Toast, Bubble } from 'weaver-mobile';
import { toJS } from 'mobx';
import * as API from './../../apis/superviseApi';

const getLabel = Tools.getLabel;
const WORKFLOW_SUPERVISE_STORE = "WORKFLOW_SUPERVISE_STORE";
export class SuperviseStore {
    @observable isShowLoading = false;
    currentBtn="batchSupervise";

    @observable  tabs = [];

    tabName = '';
    @observable popSelectKey = '';
    api_return_scopname = "";

    @observable needRemark = false;

    listAd_All = new ListAdvanced.Store({ route: '/supervise/all', id: 'all' });
    listAd_Inupervise = new ListAdvanced.Store({ route: '/supervise/insupervise', id: 'insupervise' });
    listAd_Supervised = new ListAdvanced.Store({ route: '/supervise/supervised', id: 'supervised' });
    listAd_Attention = new ListAdvanced.Store({ route: '/supervise/attention', id: 'attention' });

    @observable nowListAd = this.listAd_All;

    nowScope='all';
    layout = new RouteLayout.Store({ route: '/supervise/'+this.nowScope,id:'follow' });
    @observable cordDatas='';

    phrasesDatas = []; //批示语
    @observable remark = "";//签字意见

    queryParams = {}

    @observable visible = false;

    @observable showCheck = false;

    @observable leftTreeValueArray = []; 

    @observable flowCounts = 0;

    selected = '';

    nowTab = 0;
 
    leftTree_params = {}

    isShowTotal = true; //总数

    @action
    onSearch = (status = {}) => {
        // const { nowListAd, nowTab, leftTree_params, getTreeCount } = this.props.wfSuperviseStore;
        const { searchAdvancedStore, listViewStore, searchParams } = this.nowListAd;
        // 搜索框等搜索数据
        let params = { ...searchParams };
        // 获取高级搜索参数
        if (searchAdvancedStore) {
            const { formParams } = searchAdvancedStore.form;
            params = { ...formParams, ...this.leftTree_params };
        }
        // 清除列表数据
        listViewStore.clearData();
        if (!status.notClearSelectedRowKeys) {//清空列表已选中数据
            listViewStore.setState({ selectedRowKeys: [] })
        }
        // 带参数请求列表数据
        this.nowListAd.getData({ url: '/api/workflow/supervise/getSessionkey', method: "POST", params: { ...params, tabKey: this.nowTab, ismobile: 1, _ec_device: true } });
    }

    @action
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }

    @action
    changeTab = (obj) => {
        let tabKey = 0;
        switch (obj.path) {
            case "all":
                this.nowListAd = this.listAd_All; 
                tabKey = 0;
                break;
            case "insupervise":
                this.nowListAd = this.listAd_Inupervise;
                tabKey = 1;
                break;
            case "supervised":
                this.nowListAd = this.listAd_Supervised;
                tabKey = 2;
                break;
            case "attention":
                this.nowListAd = this.listAd_Attention;
                tabKey = 3;
                break;
            default:
                this.nowListAd = this.listAd_All;
                tabKey = 0;
                break;
        }
        this.nowTab = tabKey;
        this.nowListAd.clear();
        this.nowListAd.getData({ url: '/api/workflow/supervise/getSessionkey', method: "POST", params: { tabKey, ismobile: 1, _ec_device: true } });
        if (this.nowListAd.searchAdvancedStore) {
            this.nowListAd.searchAdvancedStore.getData({ url: "/api/workflow/supervise/getConditionInfo", method: 'POST', params: { ...this.queryParams, ismobile: 1, _ec_device: true } })
        }
        if (this.nowListAd.listViewStore) {
            this.nowListAd.listViewStore.setState({ template: this.mobileshowtemplate })
        }
        if (this.nowListAd.leftTreeStore) {
            this.nowListAd.leftTreeStore.setState({ rowKey: "domid" })
        }
        this.showCheck = false;
        this.leftTree_params = {};
    }

    @action
    init = () => {
        this.tabs = [
            { title: getLabel(82857, '全部'), path: 'all' },
            { title: getLabel(33226, '未督办'), path: 'insupervise' },
            { title: getLabel(33227, '已督办'), path: 'supervised' },
            { title: getLabel(26469, '我的关注'), path: 'attention' },
        ];
    }

    @action
    getTreeCount = () => {
        API.getTreeCount().then(result => {
            const { flowAllCounts = 0, flowOverCounts = 0} = result;
            if (this.nowTab == 0) {
                this.flowCounts = flowAllCounts;
            } else if (this.nowTab == 1) {
                this.flowCounts = flowAllCounts - flowOverCounts;
            } else if (this.nowTab == 2) {
                this.flowCounts = flowOverCounts;
            }
            this.isShowTotal = true;
        })
    }



    loadPhrasesDatas = () => {
        API.loadPhrasesDatas().then(result => {
            const { phrasesDatas = [] } = result;
            this.phrasesDatas = phrasesDatas;
            this.needRemark = true;
        })
    }
    doSupervise = (params = {}) => {
        this.isShowLoading = true;
        API.doSupervise(params).then(data => {
            this.isShowLoading = false;
            this.needRemark = false;
            this.showCheck = false;
            this.remark = '';
            const { result } = data;
            if (result) {
                this.remark = "";
                this.nowListAd.clear();
                this.nowListAd.getData({ url: '/api/workflow/supervise/getSessionkey', method: "POST", params: { tabKey:this.nowTab, ismobile: 1, _ec_device: true } });
                if (this.nowListAd.searchAdvancedStore) {
                    this.nowListAd.searchAdvancedStore.getData({ url: "/api/workflow/supervise/getConditionInfo", method: 'POST', params: { ...this.queryParams, ismobile: 1, _ec_device: true } })
                }
                if (this.nowListAd.listViewStore) {
                    this.nowListAd.listViewStore.setState({ template: this.mobileshowtemplate, selectedRowKeys: [] })
                }
                if (this.nowListAd.leftTreeStore) {
                    this.nowListAd.leftTreeStore.setState({ rowKey: "domid" })
                }
                this.showCheck = false;
                this.leftTree_params = {};
                Bubble.success(getLabel(387108,"督办成功"), 2);
            } else {
                Bubble.error(getLabel(387109,"督办失败"), 2);
            }
        })
    }
}